package edu.uwlax.cs.oayonlinestore.client.gui.salesdept;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.ProductDetailsVO;
import edu.uwlax.cs.oayonlinestore.vo.ProductPriceVO;

public class PageSalSubcatPrices extends TitlePage {

	public class ColorTextBox extends TextBox implements KeyboardListener {

		public final static String STYLE_TEXT_EDITED = "ColorTextBoxEdited";

		public ColorTextBox(String val) {
			super();
			addKeyboardListener(this);
			setText(val);
			setTextAlignment(TextBox.ALIGN_RIGHT);
			setWidth("70px");
		}

		public void onKeyDown(Widget sender, char keyCode, int modifiers) {
			setStyleName(STYLE_TEXT_EDITED);
		}

		public void onKeyPress(Widget sender, char keyCode, int modifiers) {

		}

		public void onKeyUp(Widget sender, char keyCode, int modifiers) {

		}

		public void resetStyle() {
			removeStyleName(STYLE_TEXT_EDITED);
		}

	}

	private class EditPricesTable extends DekoTable {

		public EditPricesTable() {
			super();
			addTitle("Brand");
			addTitle("Product");
			addTitle("Available");
			addTitleHTML("Price<br>(USD)");
			addTitleHTML("Normal<br>Discount<br>(%)");
			addTitleHTML("Industrial<br>Discount<br>(%)");
			addTitle("");
		}

		public void addItem(ProductDetailsVO p) {
			AddRow r = addRow();
			ColorTextBox price = new ColorTextBox(p.getPrice() + "");
			ColorTextBox ndisc = new ColorTextBox(p.getNormalDiscount() + "");
			ColorTextBox idisc = new ColorTextBox(p.getIndustrialDiscount()
					+ "");
			r.add(p.getBrand());
			r.add(p.getName());
			r.add(p.getInStockCount());
			r.add(price);
			r.add(ndisc);
			r.add(idisc);
			r.add(new Button("set", new SaveHandler(price, ndisc, idisc, p
					.getId())));
		}

	}

	private class SaveHandler extends DefaultAsyncCallback implements
			ClickListener {

		private ColorTextBox idisc;

		private ColorTextBox ndisc;

		private ColorTextBox price;

		private int id;

		public SaveHandler(ColorTextBox price, ColorTextBox ndisc,
				ColorTextBox idisc, int id) {
			super();
			this.price = price;
			this.ndisc = ndisc;
			this.idisc = idisc;
			this.id = id;
		}

		public void onClick(Widget arg0) {
			try {
				ProductPriceVO p = new ProductPriceVO();
				p.setId(id);
				p.setPrice(Float.parseFloat(price.getText()));
				p.setNormalDiscount(Float.parseFloat(ndisc.getText()));
				p.setIndustrialDiscount(Float.parseFloat(idisc.getText()));
				OnlinestoreRService.Util.getInstance()
						.setProductPrices(p, this);
			} catch (NumberFormatException e) {
				Window.alert("Invalid number format:" + e.getMessage());
			}

		}

		public void onSuccess(Object arg0) {
			price.resetStyle();
			ndisc.resetStyle();
			idisc.resetStyle();
		}
	}

	private EditPricesTable table;

	protected void create() {
		setTitle("Edit Prices and Discounts");
		add(table = new EditPricesTable());
	}

	public String getID() {
		return "EditPrices";
	}

	protected void onDataArrived(Object o) {
		ProductDetailsVO[] p = (ProductDetailsVO[]) o;
		table.cleanExceptHeader();
		for (int i = 0; i < p.length; i++)
			table.addItem(p[i]);
	}

	protected boolean onShow(int param) {
		OnlinestoreRService.Util.getInstance().getProductDetailList(param,
				getAsyncHandler());
		return true;
	}

}
